//
//  ManageFeeDetailViewController.m
//  wtshoppingcite
//
//  Created by TW on 15-4-21.
//  Copyright (c) 2015年 TW. All rights reserved.
//

#import "ManageFeeDetailViewController.h"
#import "BaseTableView.h"
#import "ManageFeeModel.h"
#import "RoomModel.h"

@interface ManageFeeDetailViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *contentTableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation ManageFeeDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArray = [[NSMutableArray alloc]initWithCapacity:1];
   
    [self setTitleView:@"选择小区"];
    
    if(_index == 0){
        [self getBuildingList];
    }else{
        [self getRoomList];
    }
    
    [self initWithTableView];
}

#pragma  mark  缴费列表
-(void)initWithTableView{
    
    _contentTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight) style:UITableViewStylePlain];
    _contentTableView.delegate = self;
    _contentTableView.dataSource = self;
    _contentTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_contentTableView];
}


#pragma mark 获取小区信息
-(void)getBuildingList{
    
    NSString *cid = [[RegisterHelper shareHelper]citesId];
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    
    SetValueForKey(listDic, cid, @"cid");
 
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                                            andAPISuffix:kBuildings
                                           andHttpMethod:@"POST"
                                           andParameters:listDic
                                         andRequestCache:NO
                                          andUseParamDic:YES
                                     andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                                   andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                                       
                                       request.requestCache = YES;
                                       
                                       if (response.isError) {
                                           
                                           DLog(@"error %@",response.errorMessage);
                                           
                                       }else{
                                           
                                           NSDictionary *responseDic = request.responseJSON;
                                           DLog(@"成功数据:%@",responseDic);
                                           
                                           NSArray *list = [responseDic objectForKey:@"buildings"];
                                           
                                           for (NSDictionary *dic in list) {
                                               
                                               ManageFeeModel *manageFeeModel = [[ManageFeeModel alloc]initWithManageFeeList:dic];
                                               [self.dataArray addObject:manageFeeModel];
                                           }
                                       }
                                       
                                       [_contentTableView reloadData];
                                   }];
}

#pragma mark 获取房间信息
-(void)getRoomList{
    
    
    NSString *buildingId = self.buildingId;
    
    NSMutableDictionary *listDic = [NSMutableDictionary dictionary];
    
    SetValueForKey(listDic, buildingId, @"buildingId");
    
    [NetWorkService byRequestWithAPIPrefix:kBaseAppUrl
                              andAPISuffix:kRooms
                             andHttpMethod:@"POST"
                             andParameters:listDic
                           andRequestCache:NO
                            andUseParamDic:YES
                       andPostDataEncoding:MKNKPostDataEncodingTypeJSON
                     andRequestFinishBlock:^(BYRequest *request, BYResponse *response) {
                         
                         request.requestCache = YES;
                         
                         if (response.isError) {
                             
                             DLog(@"error %@",response.errorMessage);
                             
                         }else{
                             
                             NSDictionary *responseDic = request.responseJSON;
                             DLog(@"成功数据:%@",responseDic);
                             
                             NSArray *list = [responseDic objectForKey:@"rooms"];
                             
                             for (NSDictionary *dic in list) {
                                 
                                 RoomModel *roomModel = [[RoomModel alloc]initWithRoomModelList:dic];
                                 [self.dataArray addObject:roomModel];
                             }
                         }
                         
                         [_contentTableView reloadData];
                     }];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _dataArray.count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"manageCell1"];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"manageCell1"];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        
        UILabel *label = [self createLabelWithFrame:CGRectMake(10, 20, kWidth-40, 20) text:@"" font:18 textAli:NSTextAlignmentLeft textColor:MAIN_BLACK_COLOR];
        label.tag = 100;
        [cell.contentView addSubview:label];
        
        UIView *seperateLine = [[UIView alloc]initWithFrame:CGRectMake(0, 50 - 1, kWidth, 1)];
        seperateLine.backgroundColor = MAIN_GRAY_COLOR;
        [cell.contentView addSubview:seperateLine];
        
    }
    
    if (_index == 0) {
        UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:100];
        
        ManageFeeModel *manageFeeModel = [_dataArray objectAtIndex:indexPath.row];
        
        textLabel.text = manageFeeModel.buildingName;
        
    }else if(_index == 1){
        
        UILabel *textLabel = (UILabel *)[cell.contentView viewWithTag:100];
        
        RoomModel *roomModel = [_dataArray objectAtIndex:indexPath.row];
        
        textLabel.text = roomModel.unitName;
    }

    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (_index == 0) {
        ManageFeeModel *mangeFeeModel = _dataArray[indexPath.row];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(manageFeeBuild:)]) {
            [self.delegate manageFeeBuild:mangeFeeModel];
        }
        [self.navigationController popViewControllerAnimated:YES];
        
    }else if(_index == 1){
        
        RoomModel *roomModel = _dataArray[indexPath.row];
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(manageFeeRoom:)]) {
            [self.delegate manageFeeRoom:roomModel];
        }
        [self.navigationController popViewControllerAnimated:YES];
    }
   
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    ;
}


- (void)viewWillDisappear:(BOOL)animated
{
    
    [super viewWillDisappear:animated];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
